<template>
    <div id="home">
        <!-- <navbar class="home-nav"><div slot="center">购物街</div></navbar> -->
        <!-- 顶部 nav-bar -->
        <van-nav-bar title="购物街" :fixed="true" :border="false" />
        <TabControl  :titles="['流行','新款','精选']"
                     @tabClick="tabClick" ref="tabControl1"
                    class="tabControl" v-show="isTabFixed"/>

        <Scroll class="content" ref="scroll" :probeType="3" @scroll="contentScroll"
        :pullUpLoad="true" @pullingUp="loadMore">
        <home-swiper :banners="banners" @swiperimageLoad="swiperimageLoad"/>
        <recommend-view :recommends="recommends"/>
        <FeatureView/>
        <TabControl  :titles="['流行','新款','精选']"
                    @tabClick="tabClick" ref="tabControl2"/> 
                    <!--  导航吸顶 -->
            
        <GoodsList :goods="showGoods"/>
        </Scroll>
        <BackTop @click.native="backClick" v-show="isShowBackTop"/>
    </div>
</template>

<script>
    import HomeSwiper from "./childComps/HomeSwiper";
    import RecommendView from "./childComps/RecommendView";
    import FeatureView from "./childComps/FeatureView";

    import Navbar from "components/common/navbar/Navbar";
    import TabControl from "components/content/TabControl";
    import Scroll from "components/common/scroll/Scroll";
    import BackTop from "components/content/backTop/BackTop";

    import {getHomeMultidata, getHomeGoods} from "network/home";
    import {itemListtenerMixin, backTopMixin} from "common/mixin";
    import GoodsList from "components/content/goods/GoodsList";



    export default {
        name: "Home",
        components: {
            HomeSwiper,
            RecommendView,
            FeatureView,
            Navbar,
            TabControl,
            GoodsList,
            Scroll,
            BackTop,
        },
        mixins: [itemListtenerMixin, backTopMixin],
        data() {
            return {
                banners: [],
                recommends: [],
                goods: {
                    'pop': {page: 0,list: []},
                    'new': {page: 0,list: []},
                    'sell': {page: 0,list: []},
                },
                currentType: 'pop',
                isShowBackTop: false,
                tabOffsetTop: 0,
                isTabFixed: false,
                saveY: 0,
                itemImglistenr: null
            }
        },
        computed: {
            showGoods() {
                return this.goods[this.currentType].list
            }
        },
       /* activated() {
            this.$refs.scroll.scrollTo(0,this.saveY,0)
        },*/
         deactivated() {
             //this.saveY = this.$refs.scroll.scroll.y
             this.$bus.$off('itemImageload',this.itemImglistenr)
         },
        created() {
            //请求多个数据
           this.getHomeMultidata();
            //请求商品数据
           this.getHomeGoods('pop');
           this.getHomeGoods('new');
           this.getHomeGoods('sell');


        },
        mounted() {
           /* //监听itemimage加载完成
            const refresh = debounce(this.$refs.scroll.refresh);

            this.itemImglistenr = () => {
                //console.log("----");
                refresh()
            }
            this.$bus.$on('itemImageload', this.itemImglistenr)*/
        },
        methods: {
            /**
             * 事件监听相关的方法
             */
            tabClick(index){
                switch (index) {
                    case 0:
                        this.currentType = 'pop';
                        break;
                    case 1:
                        this.currentType = 'new';
                        break;
                    case 2:
                        this.currentType = 'sell';
                        break;
                }
                this.$refs.tabControl1.currentIndex = index;
                this.$refs.tabControl2.currentIndex = index;
            },
            contentScroll(position) {
                this.isShowBackTop = (-position.y) > 1000
                this.isTabFixed = (-position.y) > this.tabOffsetTop
            },
            loadMore() {
                this.getHomeGoods(this.currentType)
            },
            swiperimageLoad() {
                this.tabOffsetTop =this.$refs.tabControl2.$el.offsetTop;
            },
            /**
             * 网络请求相关的方法
             */
            getHomeMultidata() {
                getHomeMultidata().then(res => {
                    //console.log(res);
                    this.banners = res.data.banner.list;
                    this.recommends = res.data.recommend.list;
                })
            },
            getHomeGoods(type) {
                const page = this.goods[type].page+1;
                getHomeGoods(type, page).then(res => {
                    this.goods[type].list.push(...res.data.list)
                    this.goods[type].page +=1

                    this.$refs.scroll.finishPullUp()
                })
            }
        }
    }
</script>

<style scoped>
    #home{
       /* padding-top: 44px;*/
        height: 100vh;
        position: relative;
    }
    .van-nav-bar{
        background-color: #FF8A9D;
    }
    .van-nav-bar__title{
        color: white;
    }
.home-nav{
  /*  position: fixed;
    left: 0;
    right: 0;
    top: 0;
    z-index: 9;*/
    background-color: var(--color-tint);
    color: #fff;
}

    .content{
        overflow: hidden;

        position: absolute;
        top: 44px;
        bottom: 49px;
        left: 0;
        right: 0;
    }
    .tabControl{
        position: relative;
        z-index: 9;
    }
    /*.fixed{
        position: fixed;
        top: 0;
        left: 0;
        right: 44px;
    }*/
  /*  .content{
        height: calc(100% - 93px);
        overflow: hidden;
        margin-top: 44px;
    }*/
</style>
